import Cookies from 'js-cookie'

const TokenKey = 'token'
const Roles = 'roles'
const userData = 'user-data-key'
const currentRole = 'currentRole'

const BussinessPlatformKey = 'bussiness-platform'


export function getToken() {
  // return Cookies.get(`${TokenKey}_`)
  return getCacheData(TokenKey)
}

export function setToken(token) {
  // return Cookies.set(`${TokenKey}_`, token)
  return setCacheData(TokenKey, token)
}

export function removeToken() {
  // return Cookies.remove(`${TokenKey}_`)
  return removeCacheData(TokenKey)
}

export function setUserData(user) {
  const userVoJson = JSON.stringify(user)
  return setCacheData(userData, userVoJson)
}

export function getUserData() {
  const userVoJSon = getCacheData(userData)
  return userVoJSon ? JSON.parse(userVoJSon) : null
}

export function getCurrentRole() {
  const currentRoleJson = getCacheData(currentRole)
  return currentRoleJson ? JSON.parse(currentRoleJson) : ''
}

export function removeCurrentRole(){
  return removeCacheData(currentRole)
}

export function getLoginBussinessPlatform() {
  return getCacheData(BussinessPlatformKey)
}

export function setCurrentRole(role) {
  const roleJson = JSON.stringify(role)
  return setCacheData(currentRole, roleJson)
}


export function setRoles(role) {
  const roleJson = JSON.stringify(role)
  return setCacheData(Roles, roleJson)
}

export function getRoles() {
  const rolesJson = getCacheData(Roles)
  return rolesJson ? JSON.parse(rolesJson) : ''
}

export function removeRoles() {
  return removeCacheData(Roles)
}

export function removeAll() {
  if (sessionStorage) {
    sessionStorage.clear()
  }
  if (Cookies) {
    Cookies.remove(TokenKey)
    Cookies.remove(Roles)
    Cookies.remove(userData)
    Cookies.remove(currentRole)
    Cookies.remove(BussinessPlatformKey)
  }
}


export function setLoginBussinessPlatform(bussinessPlatform) {
  return setCacheData(BussinessPlatformKey, bussinessPlatform)
}


/**
 * 获取缓存的数据、开发环境下使用 Cookise ，线上环境下使用 sessionStorage
 * @param {String} key
 */
function getCacheData(key) {
  return process.env.NODE_ENV === 'development' ? Cookies.get(key) : window.sessionStorage.getItem(key)
}

/**
 * 设置缓存的数据、开发环境下使用 Cookise ，线上环境下使用 sessionStorage
 * @param {String} key
 * @param {String} data
 */
function setCacheData(key, data) {
  process.env.NODE_ENV === 'development' ? Cookies.set(key, data) : window.sessionStorage.setItem(key, data)
}

/**
 * 清空缓存的数据、开发环境下使用 Cookise ，线上环境下使用 sessionStorage
 * @param {String} key
 */
function removeCacheData(key) {
  process.env.NODE_ENV === 'development' ? Cookies.remove(key) : window.sessionStorage.removeItem(key)
}

export default {
  removeCacheData, setCacheData, getCacheData, removeAll, getToken, setToken, removeToken, setUserData, getUserData, getCurrentRole, removeCurrentRole, getLoginBussinessPlatform, setCurrentRole, setRoles, getRoles,
  removeRoles, setLoginBussinessPlatform
}
